package structure.helpercalss;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @AUTHOR LYF
 * @DATE 2022/3/18
 * @VERSION 1.0
 * @DESC
 * 思考与CountDownLatch的区别
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(10);

        for(int i=0;i<10;i++)
            new Thread(()->{
                try {

                    System.out.println("hello!!"+Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(new Random().nextInt(3));
                    //思考顺序位置？---mutex加锁问题
                    System.out.println("执行结束::"+Thread.currentThread().getName());
                    cyclicBarrier.await();//
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();

        cyclicBarrier.await();
        System.out.println("执行结束...");
        //cyclicBarrier.await();
    }
}
